package com.jy.api.domain;

import com.jy.api.enums.RechargeEnum;
import com.jy.api.enums.StoreLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.util.Date;

/**
 * Created by hanxi on 2017/7/1.
 */

@Entity
@Data
@NoArgsConstructor
public class Store {
    @Id
    @Column(length = 50)
    private String id;
    private String username;
    private String name;
    private String typeCode;
    private String typeName;
    private String addr;
    private String referralCode;
    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date addTime;
    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;
    private boolean enabled = true;

    private boolean cashCoupon=false;

    private boolean purchaseEnabled=false;

    /**
     * 过期时间
     */
    private Date expireTime;

    /**
     * 支付商户号
     */
    private String mchId;
    /**
     * 店铺简介
     */
    private String summary;

    /**
     * 是否开启打赏
     */
    private boolean reward = false;

    /**
     * 店铺Logo
     */
    private String storeLogo;

    /**
     * 支付秘药
     */
    @Column(name = "`key`")
    private String key;

    /**
     * 库存是否限制销售，默认为限制
     */
    @Column(name = "is_stock_limit_sales")
    private boolean stockLimitSales = true;

    /**
     * 店员宝是否允许使用现金
     */
    @Column(name = "is_clerk_cash_sales")
    private boolean clerkCashSales = true;

    /**
     * 是否启用系统商品
     */
    @Column(name = "sys_product_enabled")
    private boolean sysProductEnabled = true;

    /**
     * 客户端下载地址
     */
    private String clientUrl;

    /**
     * 支付模式
     */
    @Enumerated(EnumType.STRING)
    private PayMode payMode = PayMode.THIRD_PARTY;

    /**
     * 推广二维码地址
     */
    @Column(name = "qrcode")
    private String QRCode;

    private long callSecond;

    private boolean zero = false;

    private boolean discount = false;

    private String psw;



    /**
     * 店员宝是否强制需要绑定会员
     */
    private boolean clerkMemberSwitch = false;

    private boolean call = false;
    @Temporal(TemporalType.TIMESTAMP)
    private Date callTime;
    @Temporal(TemporalType.TIMESTAMP)
    private Date callEndTime;
    private double rechargeLimit;


    /**
     * 店铺等级
     */
    @Enumerated(EnumType.STRING)
    private StoreLevel level = StoreLevel.THREE;

    public Store(String username, String name, String typeCode, String typeName, String addr) {
        this.username = username;
        this.name = name;
        this.typeCode = typeCode;
        this.typeName = typeName;
        this.addr = addr;
    }


    /**
     * 校验店铺是否为网吧
     *
     * @return
     */
    public boolean checkInternet() {
        return sysProductEnabled;
    }

    /**
     * 校验第三方配置
     *
     * @return
     */
    public boolean checkThreePayConfig() {
        if (StringUtils.isNotBlank(mchId)) {
            return true;
        }
        return false;
    }

    /**
     * 判断店铺启用支付方式获取支付方式
     *
     * @param channel
     * @return
     */
    public PaymentChannel getChannel(boolean channel) {
        if (payMode == PayMode.THIRD_PARTY) {
            if (channel) {
                return PaymentChannel.ALIPAY;
            } else {
                return PaymentChannel.WECHATPAY;
            }
        } else {
            if (channel) {
                return PaymentChannel.ALIPAY_CASH;
            } else {
                return PaymentChannel.WECHATPAY_CASH;
            }
        }

    }

    public String getPayNullMsg() {
        if (payMode == PayMode.THIRD_PARTY) {
            return "获取失败！请稍后重试！";
        } else {
            return "获取失败！未查询到预留二维码信息！";
        }
    }

    /**
     * 店铺充值等级校验
     *
     * @return
     */
    public String checkRechargeLevel(RechargeEnum rechargeEnum) {
        if (level == StoreLevel.ONE && rechargeEnum == RechargeEnum.UPGRADE) {
            return "店铺未购买基础版不能直接升级！";
        } else if (level == StoreLevel.THREE && rechargeEnum != RechargeEnum.THREE_LEVEL) {
            return "店铺等级不可更改！";
        }
        return "";
    }
}
